package com.jinlejia.erp.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemCustormerBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户Adapter
 */
public class CustormerAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    private String unit;

    public CustormerAdapter(ArrayList<Map<String, Object>> data,String unit) {
        super(data);
        this.unit = unit;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custormer, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ItemCustormerBinding binding =  DataBindingUtil.getBinding(viewHolder.itemView);

        HashMap<String, Object> map = (HashMap<String, Object>) mData.get(i);
        binding.customerName.setText(map.get("name").toString());
        if( map.containsKey("level") ){
            binding.ratingbar.setRating(Float.parseFloat(map.get("level").toString()));
        }else{
            binding.ratingbar.setRating(0);
        }

        if (Integer.parseInt(map.get("sex").toString()) == 0) {
            binding.customerGender.setText("女士");
            binding.RIVImg.setImageResource(R.mipmap.ic_women);
        } else {
            binding.customerGender.setText("先生");
            binding.RIVImg.setImageResource(R.mipmap.ic_man);
        }
        if( map.containsKey("tags") && !map.get("tags").equals("") ) {
            binding.tagCload.setVisibility(View.VISIBLE);
            String tagStr = map.get("tags").toString();
            ArrayList<String> tags = new ArrayList<String>();
            tags.addAll(Arrays.asList(tagStr.split(",")));
            binding.tagCload.setTags(tags);
        }else{
            binding.tagCload.setVisibility(View.GONE);
        }
       //意向房源
        JSONArray orderList = JSON.parseArray(map.get("orderList").toString());
        //意向1
        JSONObject object1 = (JSONObject) orderList.get(0);
        StringBuilder sb1 = new StringBuilder(TextUtils.isEmpty(object1.getString("orderPriceMin"))?"":object1.getString("orderPriceMin"));
        sb1.append("-").append(TextUtils.isEmpty(object1.getString("orderPriceMax"))?"":object1.getString("orderPriceMax")).append(unit).append("|")
                .append(TextUtils.isEmpty(object1.getString("orderSquareMin"))?"":object1.getString("orderSquareMin")).append("-")
                .append(TextUtils.isEmpty(object1.getString("orderSquareMax"))?"":object1.getString("orderSquareMax")).append("㎡").append("\n")
                .append(TextUtils.isEmpty(object1.getString("orderCounty"))?"":object1.getString("orderCounty")).append("  ")
                .append(TextUtils.isEmpty(object1.getString("orderArea"))?"":object1.getString("orderArea"))
                .append(TextUtils.isEmpty(object1.getString("needRemark"))?"":"\n"+object1.getString("needRemark"));
        //当区域为null时，则不显示所有的信息
        if (TextUtils.isEmpty(object1.getString("orderCounty")) || TextUtils.isEmpty(object1.getString("orderArea"))){
            sb1 = null;
            binding.intention1.setVisibility(View.GONE);
        }else {
            binding.intention1.setVisibility(View.VISIBLE);
        }
        binding.intention1.setText(sb1);
        //意向2
        JSONObject object2 = (JSONObject) orderList.get(1);
        if( !TextUtils.isEmpty(object2.getString("orderPriceMin")) && !TextUtils.isEmpty(object2.getString("orderPriceMax"))
                && !TextUtils.isEmpty(object2.getString("orderSquareMin"))  && !TextUtils.isEmpty(object2.getString("orderSquareMax"))){
            binding.intention2.setVisibility(View.VISIBLE);
            StringBuilder sb2 = new StringBuilder(TextUtils.isEmpty(object2.getString("orderPriceMin"))?"":object2.getString("orderPriceMin"));
            sb2.append("-").append(TextUtils.isEmpty(object2.getString("orderPriceMax"))?"":object2.getString("orderPriceMax")).append("万|")
                    .append(TextUtils.isEmpty(object2.getString("orderSquareMin"))?"":object2.getString("orderSquareMin")).append("-")
                    .append(TextUtils.isEmpty(object2.getString("orderSquareMax"))?"":object2.getString("orderSquareMax")).append("㎡").append("\n")
                    .append(TextUtils.isEmpty(object2.getString("orderCounty"))?"":object2.getString("orderCounty")).append("  ")
                    .append(TextUtils.isEmpty(object2.getString("orderArea"))?"":object2.getString("orderArea"))
                    .append(TextUtils.isEmpty(object2.getString("needRemark"))?"":"\n"+object2.getString("needRemark"));
            //当区域为null时，则不显示所有的信息
            if (TextUtils.isEmpty(object2.getString("orderCounty")) || TextUtils.isEmpty(object2.getString("orderArea"))){
                sb2 = null;
                binding.intention2.setVisibility(View.GONE);
            }else {
                binding.intention2.setVisibility(View.VISIBLE);
            }
            binding.intention2.setText(sb2);
        }else{
            binding.intention2.setVisibility(View.GONE);
        }

        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


}
